#include <bits/stdc++.h>

const int N = 1e5 + 10;
struct FHQ_Treap {
    int l, r, val, size, key;
} tree[N]; int cnt, root;
inline unsigned int rnd() {
    static unsigned int x = 114; return (x *= 514) %= 177777771;
}
inline void update(int nw) { tree[nw].size = tree[tree[nw].l].size + tree[tree[nw].r].size + 1; }
inline int newnode(int val) { tree[++cnt] = FHQ_Treap{0, 0, val, 1, rnd()}; return cnt; }

void split(int nw, int val, int &x, int &y) {
    if (!nw) return x = y = 0, void();
    if (tree[nw].val <= val) {
        x = nw; split(tree[nw].r, val, tree[x].r, y);
    }
    else {
        y = nw; split(tree[nw].l, val, x, tree[y].l);
    } update(nw); return;
}

int merge(int x, int y) {
    if (!x || !y) return x | y;
    if (tree[x].key > tree[y].key) {
        tree[x].r = merge(tree[x].r, y); update(x); return x;
    }
    else {
        tree[y].l = merge(x, tree[y].l); update(y); return y;
    }
}

void ins(int val) {
    int x, y; split(root, val, x, y); root = merge(merge(x, newnode(val)), y); return;
}
void del(int val) {
    int x, y, z; split(root, val, x, z); split(x, val - 1, x, y); y = merge(tree[y].l, tree[y].r);
    root = merge(merge(x, y), z); return;
}
void getrank(int val) {
    int x, y; split(root, val - 1, x, y); printf("%d\n", tree[x].size + 1); root = merge(x, y); return;
}
void getnum(int rank) {
    int nw = root;
    while (rank) {
        if (rank == tree[tree[nw].l].size + 1) break;
        else if (rank <= tree[tree[nw].l].size) nw = tree[nw].l;
        else rank -= tree[tree[nw].l].size + 1, nw = tree[nw].r;
    } printf("%d\n", tree[nw].val); return;
}
void pre(int val) {
    int x, y; split(root, val - 1, x, y); int nw = x;
    while (tree[nw].r) nw = tree[nw].r; printf("%d\n", tree[nw].val); root = merge(x, y); return;
}
void nxt(int val) {
    int x, y; split(root, val, x, y); int nw = y;
    while (tree[nw].l) nw = tree[nw].l; printf("%d\n", tree[nw].val); root = merge(x, y); return;
}

int main() {
    int n; scanf("%d", &n);
    while (n--) {
        int op, x; scanf("%d%d", &op, &x);
        switch(op) {
            case 1: ins(x); break;
            case 2: del(x); break;
            case 3: getrank(x); break;
            case 4: getnum(x); break;
            case 5: pre(x); break;
            default: nxt(x); break;
        }
    } return 0;
}
//luogu 普通平衡树